package cn.flying.cloud.system.view;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.base.common.vo.layui.XmSelect;
import cn.flying.cloud.system.common.service.CodeService;
import cn.flying.cloud.system.role.entity.SysRoleInfo;
import cn.flying.cloud.system.role.entity.SysRoleRes;
import cn.flying.cloud.system.role.service.SysRoleInfoService;
import cn.flying.cloud.system.role.service.SysRoleResService;
import cn.flying.cloud.system.service.SysRoleService;

/**
 * @description: 角色请求处理
 * @author: admin
 * @date: 2022年06月04日 21:19
 * @version: 1.0
 */
@Controller
@RequestMapping("/sys/role/")
public class SysRoleViewController {

    @Resource
    private SysRoleInfoService sysRoleInfoService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleResService sysRoleResService;
    @Resource
    private CodeService codeService;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String list(Model model) {
        Map<String, String> statusList = codeService.getCodeList("status");
        model.addAttribute("statusList", statusList);
        return "sys/role/index";
    }

    /**
     * 获取角色下拉框数据 xm-select
     *
     * @param
     * @return
     */
    @PostMapping(value = "getSelectList")
    @ResponseBody
    public Rt<List<XmSelect>> getRoleSelectList() {
        List<XmSelect> list = sysRoleService.getRoleSelectList();
        return Rt.success(list);
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("turn/edit")
    public String turnEdit(Long id, Model model) {
        if (null != id) {
            SysRoleInfo sysRoleInfo = sysRoleInfoService.getById(id);
            model.addAttribute("role", sysRoleInfo);
        } else {
            model.addAttribute("role", new SysRoleInfo());
        }
        return "sys/role/edit";
    }

    /**
     * 权限分配页面
     *
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("turn/perms")
    public String turnPerms(String roleId, Model model) {
        model.addAttribute("roleId", roleId);
        SysRoleRes roleRes = new SysRoleRes();
        roleRes.setRoleId(roleId);
        List<SysRoleRes> list = sysRoleResService.getList(roleRes);
        model.addAttribute("resources", list);
        return "sys/role/resource";
    }

//    /**
//     * 保存角色资源关联
//     *
//     * @param resources
//     * @return
//     */
//    @PostMapping(value = "perms/save")
//    @ResponseBody
//    public Rt permissionSave(@RequestBody List<SysRoleRes> resources) {
//        sysRoleResService.saveOrUpdateBatch(resources);
//        return Rt.success("保存成功！");
//    }


}

